package com.example.petstore_2.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.petstore_2.common.CommonResponse;
import com.example.petstore_2.domain.AdminCode;
import com.example.petstore_2.domain.InviteCode;
import com.example.petstore_2.domain.UserCode;
import com.example.petstore_2.mapper.AdminCodeMapper;
import com.example.petstore_2.mapper.InviteCodeMapper;
import com.example.petstore_2.mapper.UserCodeMapper;
import com.example.petstore_2.util.ALIYUNCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Random;


@Slf4j
@Service("codeService")
public class CodeService {
    @Autowired
    private UserCodeMapper userCodeMapper;
    @Autowired
    private AdminCodeMapper adminCodeMapper;
    public CommonResponse sendPhoneCodeForUser(String phoneNumber, String type) {
        String code = ALIYUNCodeUtil.sendCode(phoneNumber);
        if(code == null) {
            log.warn(phoneNumber + " for " + type + " 短信发送失败");
            return CommonResponse.creatForError("发送失败");
        }

//        判断之前是否发过短信
        QueryWrapper<UserCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phonenumber", phoneNumber);
        queryWrapper.eq("type", type);
        UserCode userCode1 = userCodeMapper.selectOne(queryWrapper);
        if(userCode1 == null){
            UserCode userCode = new UserCode();
            userCode.setCode(code);
            userCode.setPhoneNumber(phoneNumber);
            userCode.setType(type);
            userCode.setCreateTime(LocalDateTime.now());
            userCodeMapper.insert(userCode);
        }else{
            UpdateWrapper<UserCode> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("phonenumber", phoneNumber);
            updateWrapper.eq("type", type);
            updateWrapper.set("code", code);
            updateWrapper.set("createtime", LocalDateTime.now());
            userCodeMapper.update(updateWrapper);
        }
        log.info(phoneNumber + " for " + type + " 短信发送成功");
        return CommonResponse.creatForSuccess("发送成功");
    }

    public CommonResponse checkPhoneCodeForUserRegister(String phoneNumber, String code) {
        QueryWrapper<UserCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phonenumber", phoneNumber);
        queryWrapper.eq("type", "register");
//        UserCode userCode = userCodeMapper.selectOne(queryWrapper);
        List<UserCode> userCodes = userCodeMapper.selectList(queryWrapper);
//        System.out.println(userCodes);
        if(userCodes.size() > 0) {
            if (userCodes.get(0).getCode().equals(code)) {
//                System.out.println(userCodes.get(0));
                LocalDateTime current = LocalDateTime.now();
                LocalDateTime bef = userCodes.get(0).getCreateTime();
//            测试时未加十分钟内有效
                Duration dur = Duration.between(bef, current);
//                System.out.println(dur.toMinutes());
                if (dur.toMinutes() > 10) {
//                    System.out.println("过期");
                    log.warn(phoneNumber + " for register 验证码已过期");
                    return CommonResponse.creatForError("验证码已过期");
                }
                return CommonResponse.creatForSuccess();
            } else {
                log.warn(phoneNumber + " for register 验证码错误");
                return CommonResponse.creatForError("验证码错误");
            }
        }
        log.warn(phoneNumber + " for register 该手机号未发送验证码");
        return CommonResponse.creatForError("该手机号未发送验证码");
    }
    public CommonResponse sendPhoneCodeForAdmin(String phoneNumber, String type) {
        String code = ALIYUNCodeUtil.sendCode(phoneNumber);
        if(code == null) {
            log.warn(phoneNumber + " for " + type + " 短信发送失败");
            return CommonResponse.creatForError("发送失败");
        }

//        判断之前是否发过短信
        QueryWrapper<AdminCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phonenumber", phoneNumber);
        queryWrapper.eq("type", type);
        AdminCode adminCode1 = adminCodeMapper.selectOne(queryWrapper);
        if(adminCode1 == null){
            AdminCode adminCode = new AdminCode();
            adminCode.setCode(code);
            adminCode.setPhoneNumber(phoneNumber);
            adminCode.setType(type);
            adminCodeMapper.insert(adminCode);
        }else{
            UpdateWrapper<AdminCode> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("phonenumber", phoneNumber);
            updateWrapper.set("code", code);
            adminCodeMapper.update(updateWrapper);
        }
        log.info(phoneNumber + " for " + type + " 短信发送成功");
        return CommonResponse.creatForSuccess("发送成功");
    }

    public CommonResponse checkPhoneCodeForAdminRegister(String phoneNumber, String code) {
        QueryWrapper<AdminCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phonenumber", phoneNumber);
        queryWrapper.eq("type", "register");
//        UserCode userCode = userCodeMapper.selectOne(queryWrapper);
        List<AdminCode> adminCodes = adminCodeMapper.selectList(queryWrapper);
//        System.out.println(userCodes);
        if(adminCodes.size() > 0) {
            if (adminCodes.get(0).getCode().equals(code)) {
//                System.out.println(userCodes.get(0));
                LocalDateTime current = LocalDateTime.now();
                LocalDateTime bef = adminCodes.get(0).getCreateTime();
//            测试时未加十分钟内有效
                Duration dur = Duration.between(bef, current);
//                System.out.println(dur.toMinutes());
//                if (dur.toMinutes() > 10) {
////                    System.out.println("过期");
//                    return CommonResponse.creatForError("手机验证码已过期");
//                }
                return CommonResponse.creatForSuccess();
            } else {
                log.warn(phoneNumber + " for register 手机验证码错误");
                return CommonResponse.creatForError("手机验证码错误");
            }
        }
        log.warn(phoneNumber + " for register 该手机号未发送验证码");
        return CommonResponse.creatForError("该手机号未发送验证码");
    }
}
